<style type="text/css">
    .demo-upload-list {
        display: inline-block;
        width: 60px;
        height: 60px;
        text-align: center;
        line-height: 60px;
        border: 1px solid transparent;
        border-radius: 4px;
        overflow: hidden;
        background: #fff;
        position: relative;
        box-shadow: 0 1px 1px rgba(0, 0, 0, .2);
        margin-right: 4px;
    }

    .demo-upload-list img {
        width: 100%;
        height: 100%;
    }

    .demo-upload-list-cover {
        display: none;
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        background: rgba(0, 0, 0, .6);
    }

    .demo-upload-list:hover .demo-upload-list-cover {
        display: block;
    }

    .demo-upload-list-cover i {
        color: #fff;
        font-size: 20px;
        cursor: pointer;
        margin: 0 2px;
    }

    .selected-search {
        border: 1px solid #dddee1;
        height: 200px;
        width: 260px;
        position: relative;
        border-radius: 4px;
    }

    .selected-search .search-action {
        position: absolute;
        top: 10px;
        left: 10px;
        right: 10px;
    }

    .selected-search .selected-arr {
        position: absolute;
        top: 50px;
        left: 20px;
        right: 20px;
        bottom: 10px;
        overflow: scroll;
    }

    .select-click {
        border: 1px solid #dddee1;
        border-radius: 4px;
        padding: 0 10px;
        margin-bottom: 10px;
    }

    .selected-search .selected-arr .selected-item {
        /*border-bottom: 1px solid #dfdfdf;*/
    }
</style>
<template>
    <i-form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
        <Form-item label="内部编号" prop="dormcode">
            <i-input v-model="formValidate.dormcode" placeholder="请输入内部编号"></i-input>
        </Form-item>
        <Form-item label="位置" prop="location">
            <i-input v-model="formValidate.location" placeholder="请输入位置"></i-input>
        </Form-item>
        <Form-item label="入住人数" prop="peoplecount">
            <i-input v-model="formValidate.peoplecount" placeholder="请输入入住人数"></i-input>
        </Form-item>
        <Form-item label="营房使用性别" prop="sextype">
            <i-select v-model="formValidate.sextype" placeholder="请选择营房使用性别">
                <i-option value="男">男</i-option>
                <i-option value="女">女</i-option>
            </i-select>
        </Form-item>
        <Form-item label="营房类型" prop="dormtype">
            <i-select v-model="formValidate.dormtype" placeholder="请选择营房类型">
                <i-option value="住宿">住宿</i-option>
                <i-option value="办公">办公</i-option>
                <i-option value="其它用途">其它用途</i-option>
            </i-select>
        </Form-item>
        <Form-item label="房间简述" prop="comment">
            <Input v-model="formValidate.comment" type="textarea" :autosize="{minRows: 2,maxRows: 5}"
                   placeholder="请输入..."></Input>
        </Form-item>
        <Form-item>
            <i-button @click="handleSubmit('formValidate')" type="primary">提交</i-button>
            <i-button type="ghost" style="margin-left: 8px">取消</i-button>
        </Form-item>

    </i-form>

</template>
<script>
    import apiUtil from '../../assets/utils/api_util';
    import baseUtil from '../../assets/utils/base-util';

    /**
     * dormcode    营房内部编号;
     location    营房所在位置;
     peoplecount    营房标准入住人数;
     dormtype    营房类型(住宿、办公、其它用途);
     comment        营房简述
     sextype        营房使用性别
     */


    export default {
        data() {
            return {
                formValidate: {
                    //添加字段
                    dormcode: '',
                    location: '',
                    peoplecount: "1",
                    dormtype: '',
                    sextype: '',
                },
                ruleValidate: {
                    dormcode: [
                        {required: true, message: '营房内部编号不能为空', trigger: 'blur'}
                    ],
                    location: [
                        {required: true, message: '营房所在位置不能为空', trigger: 'blur'}
                    ],
                    peoplecount: [
                        {required: true, message: '请填写营房标准入住人数', trigger: 'blur'}
                    ],
                    dormtype: [
                        {required: true, message: '请选择营房类型', trigger: 'change'}
                    ],
                    sextype: [
                        {required: true, message: '请选择营房使用性别', trigger: 'change'}
                    ],
                },
            }
        },
        mounted: function () {
            this.uploadList();
        },

        methods: {
            handleSubmit(name) {

                let that = this;
                let model = this.$refs[name].model;

                this.$refs[name].validate((valid) => {
                    that.addData(model);
                })
            },

            //添加
            addData: function (model) {
                let that = this;


                let params = that.$qs.stringify(model);
                this.$Loading.start();

                apiUtil.post(that, apiUtil.urls.AddDorm, params, function (res) {
                    that.$Loading.finish();

                    that.$Modal.confirm({
                        title: '操作成功',
                        content: '请选择下一步操作',
                        okText: '继续添加',
                        cancelText: '返回列表',
                        onOk: () => {
                            that.$router.go(0);
                        },
                        onCancel: () => {
                            baseUtil.pageJump(that,'','CampList')
                        }
                    });

                });
            },

            uploadList() {
                let that = this;
                this.imageList = that.$refs.upload ? that.$refs.upload.fileList : [];
                console.log('this.imageList:', this.imageList);
                // return ;
            },

            /*
            * 图片
            * */

            //预览上传图片
            handleView(url) {
                this.seeImg = url;
                this.visible = true;
                console.log('预览照片')
            },
            //删除图片
            handleRemove(file) {
                // 从 upload 实例删除数据
                let that = this;

                that.imagePost = [];

                const fileList = this.$refs.upload.fileList;
                this.$refs.upload.fileList.splice(fileList.indexOf(file), 1);
                this.$refs.upload.fileList.forEach(function (item, i) {
                    that.imagePost.push(that.apiUtilBase + item.response.Msg);
                });
            },
            //上传成功
            handleSuccess(res, file) {
                let that = this;
                file.url = file.response.data;
                that.imagePost = [];

                console.log('this.$refs.upload:', this.$refs.upload.fileList);
                this.$refs.upload.fileList.forEach(function (item, i) {
                    let imgItem = {
                        "img": item.response.Msg
                    }
                    that.imagePost.push(imgItem);
                });
                console.log('this.imagePost:', this.imagePost)
            },
            //上传成功
            handleFormatError(file) {
                this.$Notice.warning({
                    title: '文件格式不正确',
                    desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
                });
            },
            handleMaxSize(file) {
                this.$Notice.warning({
                    title: '超出文件大小限制',
                    desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
                });
            },
            handleBeforeUpload() {
                const check = this.imageList.length < 6;
                if (!check) {
                    this.$Notice.warning({
                        title: '最多只能上传6张图片。'
                    });
                }
                return check;
            }
        }
    }
</script>
